import threading
import time

# 创建一个Event实例
event = threading.Event()
flag = True


def worker_thread(id, event):
    global flag
    while flag == True:
        while not event.is_set():
            print(f"Worker {id} is waiting...")
            event.wait()
        print(f"Worker {id} received the signal and is finishing its job.")
        time.sleep(1)
        event.clear()


def get_cmd(event):
    global flag
    while True:
        help="""
按1: 触发Event事件
按q: 退出程序
"""
        cmd = input(help)
        if cmd == "1":
            event.set()
        elif cmd == 'q':
            print("退出程序")
            flag = False
            event.set()
            break
        else:
            print(f"不支持命令: {cmd}")


def main_thread():
    t1 = threading.Thread(target=worker_thread, args=(1, event))
    t1.start()
    get_cmd(event)

    # 等待所有工作线程完成
    t1.join()


if __name__ == "__main__":
    main_thread()
